const express = require('express');
const app = express();
const path = require('path');
const cors = require('cors');
// 引入路由
const apiRouter = require('./routes/index');
const uploadRoutes = require('./routes/uploladRoutes');  // 注意文件名


app.use(cors());
// 中间件
app.use(express.json({ limit: '300mb' }));
app.use(express.urlencoded({ extended: true, limit: '300mb' }));

// 静态文件服务
app.use('/public', express.static(path.join(__dirname, 'public')));
// 静态文件服务
app.use(express.static(path.join(__dirname, 'blog')));  // 添加这行

// API 路由
app.use('/api', apiRouter);
app.use('/api/upload', uploadRoutes);

// 根路由处理
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'blog', 'index.html'));
});

// 错误处理
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).json({ error: err.message });
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});